home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / init.c < prev    next >
Text File  |  1991-02-25  |  6KB  |  242 lines

  1. /*______________________________________________________________________
  2.  
  3.     init.c - Initialization Module
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8.     
  9.     This module takes care of program intialization.
  10. _____________________________________________________________________*/
  11.  
  12. #include    <string.h>
  13.  
  14. #include "utl.h"
  15. #include "rez.h"
  16. #include "glob.h"
  17. #include "misc.h"
  18. #include "main.h"
  19. #include "pref.h"
  20. #include "help.h"
  21. #include "abou.h"
  22. #include "init.h"
  23. #include "prog.h"
  24.  
  25. #define nil 0
  26. #define _ShutDown 0xA895
  27. #define _Unimplemented    0xA89F
  28.  
  29. /*______________________________________________________________________
  30.  
  31.     MyGrowZone - Grow Zone Procedure.
  32.     
  33.     Entry:    cbNeeded = number of bytes needed.
  34. _____________________________________________________________________*/
  35.  
  36.  
  37. static pascal long MyGrowZone (Size cbNeeded)
  38.  
  39. {
  40.  
  41.     if (NoMemFullAlert) return 0;
  42.     misc_Notify(requiresAttn, true);
  43.     (void) utl_StopAlert(noMemID, nil, 0);
  44.     ExitToShell();
  45. };
  46.  
  47.  
  48. /*______________________________________________________________________
  49.  
  50.     init_InitMem - Initialize Memory.
  51. _____________________________________________________________________*/
  52.  
  53.  
  54. void init_InitMem (void)
  55.  
  56. {
  57.     /* Increase stack space by 20,000 bytes */
  58.     
  59.     SetApplLimit(GetApplLimit() - 20000);
  60.     
  61.     /* Expand the application heap zone to the maximum. */
  62.     
  63.     MaxApplZone();
  64.     
  65.     /* Allocate a bunch of master pointers. */
  66.     
  67.     MoreMasters();
  68.     MoreMasters();
  69.     MoreMasters();
  70.     MoreMasters();
  71. };
  72.  
  73.  
  74. /*______________________________________________________________________
  75.  
  76.     init_Initialize - Initialize the Program.
  77.     
  78.     Ths routine is put in its own segment, which is unloaded after
  79.     it's used.
  80. _____________________________________________________________________*/
  81.  
  82.  
  83. void init_Initialize (void)
  84.  
  85. {
  86.     Handle            rectHandle;            /* handle to rectangle list */
  87.     short                i;                        /* loop index */
  88.     FCBPBRec            fcbBlock;            /* file info param block */
  89.     EventRecord        event;                /* event record */
  90.     Str255            apName;                /* name of Disinfectant application file */
  91.     short                apRefNum;            /* refnum of Disinfectant applicaion file */
  92.     Handle            apParam;                /* application parameters */
  93.     FInfo                fndrInfo;            /* Finder info */
  94.     
  95.     /* Initialize all the managers. */
  96.  
  97.     InitGraf((Ptr)&thePort);
  98.     InitFonts();
  99.     FlushEvents(everyEvent, 0);
  100.     InitWindows();
  101.     InitMenus();
  102.     TEInit();
  103.     InitDialogs(nil);
  104.     
  105.     /* Establish a grow zone procedure to catch out of memory conditions. */ 
  106.     
  107.     CouldAlert(noMemID);
  108.     SetGrowZone((ProcPtr)MyGrowZone);
  109.     
  110.     /* Call WaitNextEvent a few times to force our window to initially appear
  111.         in the foreground.  See TN 180. */
  112.         
  113.     utl_WaitNextEvent(everyEvent, &event, 0, nil);
  114.     utl_WaitNextEvent(everyEvent, &event, 0, nil);
  115.     
  116.     /* Check to make sure we have HFS. */
  117.     
  118.     if (FSFCBLen <= 0) {
  119.         (void) utl_StopAlert(noHFSID, nil, 0);
  120.         ExitToShell();
  121.     };
  122.     
  123.     /* Get the ROM environment. */
  124.     
  125.     OldRom = utl_Rom64();
  126.     
  127.     /* Get the file ref nums of the system file and Sample. */
  128.     
  129.     SysRefNum = SysMap;
  130.     DfectRefNum = CurMap;
  131.     DfectVol = utl_GetApplVol();
  132.     
  133.     /* Get the system volume reference number and the directory id of
  134.         the blessed folder. */
  135.         
  136.     SysVol = utl_GetSysVol();
  137.     if (!utl_VolIsMFS(SysVol)) {
  138.         fcbBlock.ioNamePtr = nil;
  139.         fcbBlock.ioVRefNum = 0;
  140.         fcbBlock.ioRefNum = SysRefNum;
  141.         fcbBlock.ioFCBIndx = 0;
  142.         (void) PBGetFCBInfo(&fcbBlock, false);
  143.         SysDirID = fcbBlock.ioFCBParID;
  144.     };
  145.     
  146.     /* Check to make sure all the packages we need exist.  If they don't 
  147.         exist, put up an alert telling the user we require system 3.2 or 
  148.         later and exit to shell. */
  149.         
  150.     if (!utl_CheckPack(listMgr, false) || !utl_CheckPack(stdFile, false) || 
  151.         !utl_CheckPack(dskInit, false) || !utl_CheckPack(intUtil, false) || 
  152.         !utl_CheckPack(bdConv, false)) {
  153.         (void) utl_StopAlert(badSysID, nil, 0);
  154.         ExitToShell();
  155.     };
  156.     
  157.     /* Determine whether or not the system has the shutdown trap.
  158.         (See TN 156).*/
  159.     
  160.     SysHasShutDown = NGetTrapAddress(_ShutDown & 0x3ff, ToolTrap) !=
  161.         NGetTrapAddress(_Unimplemented & 0x3ff, ToolTrap);
  162.         
  163.     /* Get the long sleep time. */
  164.     
  165.     LongSleep = utl_GetLongSleep();
  166.     
  167.     /* Get the cursors. */
  168.     
  169.     Watch = GetCursor(watchCursor);
  170.     HelpCurs = GetCursor(helpCursID);
  171.     IBeamCurs = GetCursor(iBeamCursor);
  172.     
  173.     /* Initialize the menus. */
  174.  
  175.     SetMenuBar(GetNewMBar(mBarID));
  176.     AddResMenu(GetMHandle(appleMID), (ResType) 'DRVR');
  177.     DrawMenuBar();
  178.     
  179.     /* Initialize the drag rectangle. */
  180.     
  181.     DragRect = (**GrayRgn).rgnBBox;
  182.     InsetRect(&DragRect, dragSlop, dragSlop);
  183.     
  184.     /* Get the rectangle list. */
  185.     
  186.     rectHandle = GetResource(rectType, rectID);
  187.     HLock(rectHandle);
  188.     (void) memcpy(RectList, *rectHandle, sizeof(Rect)*numRects);
  189.     ReleaseResource(rectHandle);
  190.     
  191.     /* Check Disinfectant's bundle bit.  If it's not set, set the bundle bit
  192.         and clear the inited bit.  This tells the Finder to try again to read
  193.         the bundle.
  194.         
  195.         This code is present because with version 2.0 some users reported that
  196.         the Disinfectant file appeared on their desktop with no icon after being
  197.         unstuffed.
  198.         
  199.         We only do this if the file has type APPL, to avoid setting the bundle bit
  200.         on Think C project files in the Think C sample program.  (Thanks, Ephraim!) */
  201.     
  202.     GetAppParms(apName, &apRefNum, &apParam);
  203.     if (!GetFInfo(apName, 0, &fndrInfo) && !(fndrInfo.fdFlags & fHasBundle) &&
  204.         fndrInfo.fdType == 'APPL') {
  205.         fndrInfo.fdFlags |= fHasBundle;
  206.         fndrInfo.fdFlags &= ~fInited;
  207.         SetFInfo(apName, 0, &fndrInfo);
  208.     };
  209.  
  210.     /* Initialize the preferences. */
  211.     
  212.     misc_ReadPref();
  213.     
  214.     /* Reopen windows, in back-to-front order. */
  215.     
  216.     for (i = Prefs.numOpenWind-1; i >= 0; i--) {
  217.         switch (Prefs.openWind[i]) {
  218.             case mainWind:
  219.                 main_Open();
  220.                 break;
  221.             case helpWind:
  222.                 help_Open(0);
  223.                 break;
  224.             case prefWind:
  225.                 pref_Open();
  226.                 break;
  227.             case abouWind:
  228.                 abou_Open();
  229.                 break;
  230.         };
  231.         while (EventAvail(updateMask | activMask, &event)) prog_Event();
  232.     };
  233.     
  234.     /* Initialize the cursor. */
  235.  
  236.     InitCursor();
  237.     
  238.     /* Set initialization complete. */
  239.     
  240.     Initialized = true;
  241. }
  242.